/**********************************************************************************
* CLASS
*		Thread
*
* DESCRIPTION
*                Thread for running command animation that genearate from Animation control.
* 		Contend manipulate function set:
*           - addAnimationControl
*           - removeAnimationControl
*			- startThread
*           - stopThread
*           - run
*
* REVISION HISTORY
*		Revision 1.0 2008/09/02 Atthaboon S.
*		First Release
*
**********************************************************************************/
var AnimationThread = Class.create({

    /****************************************************
	* FUNCTION
	*		initialize
	*
	* DESCRIPTION
	* 		initialize the Genesis object.
	*
	* ARGUMENTS
	*
	****************************************************/
  	initialize: function(id)
  	{
        this.id = id;
		this.timer;
		
		this.genesis_container = new Array();
       	this.fps		  = 12; //12 frame per second equal to flash for web site.
  	},

    /****************************************************
	* FUNCTION
	*		addGenesis
	*
	* DESCRIPTION
	* 		Add Gensis for execute in Thread running.
	*
	* ARGUMENTS
	*		genesis		   :	    instance of object genesis.
    *
	****************************************************/
    addGenesis : function(genesis)
	{
        this.genesis_container[this.genesis_container.length] = genesis;
    },

    /****************************************************
	* FUNCTION
	*		removeGenesis
	*
	* DESCRIPTION
	* 		Remove Genesis from execute in Thread running.
	*
	* ARGUMENTS
	*		index		:	    index of object genesis that you want to remove.
    *
	****************************************************/
    removeGenesis : function(index)
	{
		array_remove(this.genesis_container,index);
    },    
    
    /****************************************************
	* FUNCTION
	*		startThread
	*
	* DESCRIPTION
	* 		Start Thread running.
	*
	* ARGUMENTS
	*
	****************************************************/
    startThread: function()
	{
		//verify Genesis for loading complete
		
		
		var date  		= new Date();
        this.timer 		= setTimeout(this.id+'.runThread('+date.getUTCMilliseconds()+')', Math.round(1000/this.fps));
    },

    /****************************************************
	* FUNCTION
	*		run
	*
	* DESCRIPTION
	* 		Thread running.
	*
	* ARGUMENTS
	*		milisec :	date time in milisecond (UTC format).
	*
	****************************************************/
    runThread: function(milisec)
	{
		for(var item = 0;item<this.genesis_container.length;item++)
		{
			this.genesis_container[item].executeCommandbyLoop();
		}
	
		var endTime   = new Date();
    	var pauseTime = Math.round(1000/this.fps);    	
    	    	
    	if(milisec > endTime.getUTCMilliseconds())
    	{
			pauseTime = pauseTime - ((endTime.getUTCMilliseconds()+1000) - milisec);
    	}
    	else
    	{
    		pauseTime = pauseTime - (endTime.getUTCMilliseconds() - milisec);
    	}
    	
		if(pauseTime < 0)
		{
			// The thread can't run in the time then run next thread at pause time 0.
			this.timer = setTimeout(this.id+'.runThread('+endTime.getUTCMilliseconds()+')', 0);
		}
		else
		{
			this.timer = setTimeout(this.id+'.runThread('+endTime.getUTCMilliseconds()+')', pauseTime);
		}
    }
});

var Thread = new AnimationThread("Thread");
//Add timeout for all command success loading
setTimeout('Thread.startThread()',500);
